package com.xej.seat.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xej.seat.mapper.SeatMapper;
import com.xej.seat.model.SeatingType;
import com.xej.seat.seating.Seating;
import com.xej.seat.service.CustomerService;
import com.xej.seat.service.HallService;
import com.xej.seat.service.SeatService;
import com.xej.seat.util.DoubleUtil;
import com.xej.seat.vo.Customer;
import com.xej.seat.vo.Hall;
import com.xej.seat.vo.ManualSeat;
import com.xej.seat.vo.RequestSeat;
import com.xej.seat.vo.Seat;

/**
 * @author lisheng
 */
@Controller
public class PageController {
	
	@Resource
	private CustomerService customerService;
	
	@Resource
	private SeatService seatService;
	
    @Resource
    private SeatMapper seatMapper;
    
	@Autowired
	private Seating Seating;
	
    /**
     * 跳转到登录页面
     * @return
     */
    @RequestMapping("/login")
    public String loginPage(){
        return "login";
    }
    
	@RequestMapping("/seatSortSelf")
    @ResponseBody
    public JSONObject seatSortSelf() {
    	// 1：获取座位
    	List<Seat> seatList = seatService.loadSeatList();
    	// 2：获取需要排位的座位信息
    	List<Customer> cusList = customerService.loadCustomer();
    	if(seatList != null && seatList.size() > 0 && cusList != null && cusList.size() > 0) {
    		// TODO 5：刨去预留的的座位信息
    		// TODO 6：获取座位标签，根据标签对应的实现类对座位进行排序
			for(int i=0; i<seatList.size() && i<cusList.size(); i++) {
				Customer customer = cusList.get(i);
				seatList.get(i).setCusId(customer.getCusId());
				//seatList.get(i).setCustomer(customer);
				seatList.get(i).setName(customer.getCusName());
				seatList.get(i).setTitle(customer.getCusPosit());
				seatList.get(i).setOrg(customer.getInstName());
			}
    	}
    	// TODO 3：根据资产、职位等因素计算出每个用户的权重
    	
    	
    	// 6：根据前排优先、靠主席台优先等规则计算出座位的权重（需要设计成用户权重一致？）
    	// 7：将用户对应的放入座位中
    	Map<String,Object> map =  new HashMap<String,Object>();
    	map.put("rule", "1");
    	map.put("list", seatList);
    	return JSONObject.parseObject(JSON.toJSONString(map));
    }
}
